import Vue from "vue";
import VueRouter from "vue-router";
import store from "./store/store";
import jwt from "jsonwebtoken";
import moment from "moment";
// import Home from './views/Home.vue'
const Home = () => import("./views/Home");
const Login = () => import("./views/Login");
const Reg = () => import("./views/Reg");
const Forget = () => import("./views/Forget");
const Index = () => import("./views/channels/Index");
const Template = () => import("./views/channels/Template1");
const test = () => import("./views/test");
const Listadd = () => import("./views/Listadd");
const Center = () => import("./views/Center");
const UseCenter = () => import("./components/user/Center");
const Setting = () => import("./components/user/Setting");
const Msg = () => import("./components/user/Msg");
const Others = () => import("./components/user/Others");
const Post = () => import("./components/user/Post");
const User = () => import("./views/User");
const Accounts = () => import("./components/user/common/Accounts");
const Myinfo = () => import("./components/user/common/Myinfo");
const Passwd = () => import("./components/user/common/Passwd");
const PicUpload = () => import("./components/user/common/PicUpload");
const Mycollection = () => import("./components/user/common/Mycollection");
const Mypost = () => import("./components/user/common/Mypost");
const NotFound = () => import("./views/NotFound");
const Confirm = () => import("./views/Confirm");
const Reset = () => import("./views/Reset");
const Add = () => import("./components/contest/Add");
const Edit = () => import("./components/contest/Edit");
const Detail = () => import("./components/contest/Detail");

Vue.use(VueRouter);

const routes = [
  {
    path: "/login",
    component: Login,
    name: "login",
  },
  {
    path: "/confirm",
    component: Confirm,
    name: "confirm",
  },
  {
    path: "/reset",
    component: Reset,
    name: "reset",
  },
  {
    path: "/",
    component: Home,
    children: [
      {
        path: "",
        component: Index,
        name: "index",
      },
      {
        path: "/index/:catalog",
        component: Template,
        name: "catalog",
      },
    ],
  },
  {
    path: "/reg",
    component: Reg,
    name: "reg",
    beforeEnter: (to, from, next) => {
      if (from.name === "login") {
        next();
      } else {
        next("./login");
      }
    },
  },
  {
    path: "/forget",
    component: Forget,
    name: "forget",
  },
  {
    path: "/test",
    component: test,
    name: "test",
  },
  {
    path: "/add2",
    component: Listadd,
    name: "listadd",
  },
  {
    path: "/user/:uid",
    component: User,
    name: "home",
    prop: true,
  },
  {
    path: "/center",
    component: Center,
    linkExactActiveClass: "layui-this",
    meta: { requiresAuth: true },
    children: [
      {
        path: "",
        name: "center",
        component: UseCenter,
      },
      {
        path: "/setting",
        component: Setting,
        children: [
          {
            path: "",
            name: "myinfo",
            component: Myinfo,
          },
          {
            path: "/setting/accounts",
            name: "accounts",
            component: Accounts,
          },
          {
            path: "/setting/passwd",
            name: "passwd",
            component: Passwd,
          },
          {
            path: "/setting/picupload",
            name: "picupload",
            component: PicUpload,
          },
        ],
      },
      {
        path: "/others",
        name: "others",
        component: Others,
      },
      {
        path: "/posts",
        component: Post,
        children: [
          {
            path: "",
            name: "mypost",
            component: Mypost,
          },
          {
            path: "/posts/mycollection",
            name: "mycollection",
            component: Mycollection,
          },
        ],
      },
      {
        path: "/msg",
        name: "msg",
        component: Msg,
      },
    ],
  },
  {
    path: "/404",
    component: NotFound,
  },
  {
    path: "*",
    redirect: "/404",
  },
  {
    path: "/add",
    name: "add",
    component: Add,
  },
  {
    path: "/edit/:tid",
    props: true,
    name: "edit",
    component: Edit,
    meta: { requiresAuth: true },
  },
  {
    path: "/detail/:tid",
    name: "detail",
    component: Detail,
    meta: {
      types: ["get", "add", "edit", "delete"], //通过后台接口动态添加路由addRoutes
    },
  },
];
const router = new VueRouter({
  routes,
  linkExactActiveClass: "layui-this",
  mode: "history",
});

router.beforeEach((to, from, next) => {
  const token = localStorage.getItem("usertoken");
  const payload = jwt.decode(token);
  const userInfo = JSON.parse(localStorage.getItem("userInfo"));
  // console.log(moment().isBefore(moment(payload.exp * 1000)));
  //退出后清除了lochost 无法读到 暂时关闭
  // if(moment().isBefore(moment(payload.exp * 1000))){
  //   store.commit('setToken', token)
  //   store.commit('setUserInfo', userInfo)
  //   store.commit('setIslogin', true)
  // }else{
  //   localStorage.clear()
  // }
  if (token !== "" && token !== null) {
    store.commit("setToken", token);
    store.commit("setUserInfo", userInfo);
    store.commit("setIslogin", true);
    if (!store.state.ws) {
      store.commit("initWebSock", {});
    }
    next();
  }
  if (to.matched.some((record) => record.meta.requiresAuth)) {
    const isLogin = store.state.isLogin;
    // if(moment().isBefore(moment(payload.exp * 1000))){
    //   store.commit('setToken', token)
    //   store.commit('setUserInfo', userInfo)
    //   store.commit('setIslogin', true)
    // }else{
    //   localStorage.clear()
    // }
    if (isLogin) {
      next();
    } else {
      next("/login");
    }
  } else {
    //公工页面
    next();
  }
});

// if(to.name === 'login'){
//   next()
//   return
// }
// console.log('TCL',from);
//   const isLogin = store.state.isLogin
//   if(isLogin){
//     next()
//   }else{
//     const token = localStorage.getItem('usertoken')
//     const userInfo = JSON.parse(localStorage.getItem('userInfo'))
//     if(token !=='' && token !== null){
//       store.commit('setToken', token)
//       store.commit('setUserInfo', userInfo)
//       store.commit('setIslogin', true)
//       next()
//     }else{
//       next('/login')
//     }
//   }
//   next()

export default router;

// export default new Router({
//   routes: [
//     {
//       path: '/',
//       name: 'home',
//       component: Home
//     },
//     {
//       path: '/login',
//       name: 'login',
//       // route level code-splitting
//       // this generates a separate chunk (about.[hash].js) for this route
//       // which is lazy-loaded when the route is visited.
//       component: () => import(/* webpackChunkName: "about" */ './views/Login.vue')
//     }
//   ]
// })
